package cn.ac.iscas.base.utils;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
//import shell.Ssh2Pool;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class SshUtils {
 
    private Connection conn;
    private String ipAddr;
    private Charset charset = StandardCharsets.UTF_8;
    private String userName;
    private String password;
 
    public SshUtils(String ipAddr, String userName, String password, Charset charset) {
        this.ipAddr = ipAddr;
        this.userName = userName;
        this.password = password;
        if (charset != null) {
            this.charset = charset;
        }
    }
 
    /**
     * 登录远程Linux主机
     *
     * @return 是否登录成功
     */
    private boolean login() {
        conn = new Connection(ipAddr);
        try {
            // 连接
            conn.connect();
            // 认证
            return conn.authenticateWithPassword(userName, password);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
 
 
    /**
     * 执行Shell脚本或命令
     *
     * @param cmds 命令行序列
     * @return 脚本输出结果
     */
//    public StringBuilder exec(String cmds) throws IOException {
//        InputStream in = null;
//        InputStream errorIs = null;
//        StringBuilder result = new StringBuilder();
//        try {
//            if (this.login()) {
//                // 打开一个会话
//                Session session = conn.openSession();
//                session.execCommand(cmds);
//                in = session.getStdout();
//                errorIs = session.getStderr();
//                result = this.processStdout(in, this.charset);
//                conn.close();
//            }
//        } finally {
//            if (null != in) {
//                in.close();
//            }
//        }
//        return result;
//    }

    public Map<String, String>  exec(String cmds) throws IOException {
        Map<String, String> result = new HashMap<>(4);
        try {
            if (this.login()) {
                // 打开一个会话
                Session session = conn.openSession();
                session.execCommand(cmds);
                result.put("out", this.processStdout(session.getStdout(), charset));
                result.put("err", this.processStdout(session.getStderr(), charset));
                conn.close();
            }
        } finally {

        }
        return result;
    }


    /**
     * 执行Shell脚本或命令
     *
     * @param cmds 命令行序列
     * @return 脚本输出结果
     */
//    public InputStream run(String cmds) throws IOException {
//        InputStream in = null;
//        StringBuilder result = new StringBuilder();
//        try {
//            if (this.login()) {
//                // 打开一个会话
//                Session session = conn.openSession();
//                session.execCommand(cmds);
//                in = session.getStdout();
//                result = this.processStdout(in, this.charset);
//                conn.close();
//            }
//        } finally {
//            if (null != in) {
//                in.close();
//            }
//        }
//        return result;
//    }
 
    /**
     * 解析流获取字符串信息
     *
     * @param in      输入流对象
     * @param charset 字符集
     * @return 脚本输出结果
     */
//    public StringBuilder processStdout(InputStream in, Charset charset) throws FileNotFoundException {
//        byte[] buf = new byte[1024];
//        StringBuilder sb = new StringBuilder();
//        try {
//            // 此方法是休息10秒后最后一次性输出2行数据
//            int length;
//            while ((length = in.read(buf)) != -1) {
//                sb.append(new String(buf, 0, length));
//            }
//
//            // 这个会按照脚本一步一步执行，中途有休息10秒。
//            BufferedReader reader = null;
//            String result = null;
//            reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
//            while ((result = reader.readLine()) != null) {
//                System.out.println(result);
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return sb;
//    }

    public String processStdout(InputStream in, Charset charset) throws FileNotFoundException {
        byte[] buf = new byte[1024];
        StringBuilder sb = new StringBuilder();
        try {
            // 此方法是休息10秒后最后一次性输出2行数据
            int length;
            while ((length = in.read(buf)) != -1) {
                sb.append(new String(buf, 0, length));
            }

            // 这个会按照脚本一步一步执行，中途有休息10秒。
            BufferedReader reader = null;
            String result = null;
            reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            while ((result = reader.readLine()) != null) {
                System.out.println(result);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
 
    // 脚本执行结束返回
    public static void main(String[] args) throws IOException {
//        SSHTool tool = new SSHTool("192.168.80.202", "root", "123456", StandardCharsets.UTF_8);
        SshUtils tool = new SshUtils("172.16.10.110", "root", "Isc@s123", StandardCharsets.UTF_8);
        System.out.println(tool.exec("ls"));
    }

}
